//
//  TestCoreImageController.swift
//  studyGLKit
//
//  Created by 虞明义 on 2017/9/7.
//  Copyright © 2017年 yumingyi. All rights reserved.
//

import UIKit
import CoreImage

class TestCoreImageController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        
//        view.addSubview(oldIconV)
        view.addSubview(newIconV)
//        test()
        
        let tap = UITapGestureRecognizer.init(target: self, action: #selector(tapAction))
        tap.numberOfTapsRequired = 2
        newIconV.addGestureRecognizer(tap)
    }
    
    func tapAction(tap:UITapGestureRecognizer){
        let vc = ViewController()
        vc.getImageAction = {
            (image) in
            self.newIconV.image = image
        }
        present(vc, animated: true, completion: nil)
    }
    
    fileprivate lazy var oldIconV:UIImageView = {
        let icon = UIImageView.init(frame: CGRect.init(x: 10, y: 100, width: 80, height: 10))
        icon.image = UIImage.init(named: "test.jpg")
        icon.contentMode = .scaleAspectFill
        return icon
    }()
    
    fileprivate lazy var newIconV:UIImageView = {
        let icon = UIImageView.init(frame: CGRect.init(x: 0, y: 0, width: UIScreen.main.bounds.size.width, height: UIScreen.main.bounds.size.height))
//        icon.backgroundColor = UIColor.lightGray
        icon.contentMode = .scaleAspectFit
        icon.isUserInteractionEnabled = true
        return icon
    }()
    
    
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        let vc = ViewController()
        vc.getImageAction = {
            (image) in
            self.newIconV.image = image
        }
        present(vc, animated: true, completion: nil)
//        if let image = oldIconV.image?.cgImage{
//            test2(image: image)
//        }
    }
    
    
    func test2(image:CGImage){
        let size = CGSize.init(width: image.width, height: image.height)
        UIGraphicsBeginImageContext(size)
        print(size)
        if let context = UIGraphicsGetCurrentContext(){
            context.draw(image, in: CGRect.init(origin: CGPoint.zero, size: size))
            context.move(to: CGPoint.init(x: 100, y: 150))
            context.addLine(to: CGPoint.init(x: 380, y: 120))
            context.addLine(to: CGPoint.init(x: 330, y: 560))
            context.addLine(to: CGPoint.init(x: 120, y: 400))
            context.setStrokeColor(UIColor.white.cgColor)
            context.closePath()
            context.clip()
            let newImage = UIGraphicsGetImageFromCurrentImageContext()
            UIGraphicsEndImageContext()
            newIconV.image = newImage
        }

    }
    
    
    //*************************
    func test(){
        let color:CIColor = CIColor.init(red: 1, green: 0, blue: 0)
        print(color.colorSpace)
        let image = UIImage.init(named: "test.jpg")
        let myCIImage = CIImage.init(image: image!)
        let detector = CIDetector.init(ofType: CIDetectorTypeRectangle, context: nil, options: [CIDetectorAccuracy:CIDetectorAccuracyHigh])
        let features = detector?.features(in: myCIImage!)
//        print(features?.count)
        for feature in features!{
            print(feature.bounds)
            print(feature.type)
        }
    }

}
